package com.bprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bprogram.Enum.RelationShip;
import com.bprogram.controller.UserFeign;
import com.bprogram.mapper.SubscribeMapper;
import com.bprogram.pojo.Subscribe;
import com.bprogram.pojo.User;
import com.bprogram.service.SubscribeService;


import com.bprogram.utils.IdGenerator;
import com.bprogram.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.HashMap;


/**
 * <p>
 * 订阅关注表 服务实现类
 * </p>
 *
 * @author cai
 * @since 2023-11-21
 */
@Service
public class SubscribeServiceImpl extends ServiceImpl<SubscribeMapper, Subscribe> implements SubscribeService {
    @Autowired

    private RedisUtil redisUtil;

    @Autowired

    private UserFeign userFeign;
    @Autowired
    private SubscribeMapper subscribeMapper;

    @Override
    public boolean saveSubscribe(Subscribe subscribe) {
        User loginUser = userFeign.getLoginUser();
        if (userFeign.getLoginUser().getUserId().equals(subscribe.getUpId())) {
            Assert.isTrue(false, "禁止关注自己！");
        }
//查看被关注人和当前登录人的关系

        boolean isFans = redisUtil.sHasKey(loginUser.getUserId() + "fansId", subscribe.getUpId());
        boolean isUp = redisUtil.sHasKey(loginUser.getUserId() + "upId", subscribe.getUpId());
        QueryWrapper<Subscribe> wrapper = new QueryWrapper<>();
        Subscribe updateSubscribe = new Subscribe();
        if (!isFans && !isUp) {
            IdGenerator idGenerator = new IdGenerator();
            long id = idGenerator.snowflakeId();
            subscribe.setSubscribeId(id);
            subscribe.setFansId(loginUser.getUserId());
            subscribe.setFollow("1");
            subscribe.setAssociation("user");
            redisUtil.sSet(loginUser.getUserId() + "upId", subscribe.getUpId());
            redisUtil.sSet(subscribe.getUpId() + "fansId", loginUser.getUserId());
            //定义谁先关注的是谁
            redisUtil.sSet(loginUser.getUserId() + "follow", subscribe.getUpId());

            return this.save(subscribe);
        }
        //当前登录人的粉丝ids中有关注者的id
        if (isFans) {
            updateSubscribe.setFollow("3");
            boolean loginFirst = redisUtil.sHasKey(loginUser.getUserId() + "follow",subscribe.getUpId());
            if (loginFirst) {
                wrapper.eq("upId", subscribe.getUpId());
                wrapper.eq("fansId", loginUser.getUserId());
            }
            wrapper.eq("upId", loginUser.getUserId());
            wrapper.eq("fansId", subscribe.getUpId());
            redisUtil.sSet(loginUser.getUserId() + "upId", subscribe.getUpId());
            redisUtil.sSet(subscribe.getUpId() + "fansId", loginUser.getUserId());
        }
        //关注的ids中有当前登录人的id
        if (isUp) {
            Assert.isTrue(false, "禁止重复关注");
        }

        return this.update(updateSubscribe, wrapper);

    }

    @Override
    public boolean cancelSubscribe(long upId) {
        Subscribe subscribe = new Subscribe();
        User loginUser = userFeign.getLoginUser();
        boolean isFans = redisUtil.sHasKey(loginUser.getUserId() + "fansId", upId);
        boolean isUp = redisUtil.sHasKey(loginUser.getUserId() + "upId", upId);
        //是否当前登录人先关注对方
        boolean loginFirst = redisUtil.sHasKey(loginUser.getUserId() + "follow",upId);
//删除存在redis中两人对应的关注关系
        redisUtil.setRemove(loginUser.getUserId() + "upId", upId);
        redisUtil.setRemove(upId + "fansId", loginUser.getUserId());
        //根据特定字段和和值删除
        HashMap<String, Object> deleteMap = new HashMap<>();
        //互相关注，当前登录人取消关注
        if (isFans && isUp) {
            QueryWrapper<Subscribe> wrapper = new QueryWrapper<>();
            if (loginFirst) {
                wrapper.eq("upId", upId);
                wrapper.eq("fansId", loginUser.getUserId());
                subscribe.setFollow("2");

            } else {
                wrapper.eq("upId", loginUser.getUserId());
                wrapper.eq("fansId", upId);
                subscribe.setFollow("1");
            }


            return this.update(subscribe, wrapper);
        }
        //对方未关注自己
        if (!isFans) {
            //是
            if (loginFirst) {
                deleteMap.put("upId", upId);
                deleteMap.put("fansId", loginUser.getUserId());
                redisUtil.setRemove(loginUser.getUserId() + "follow",upId);
            } else {
                deleteMap.put("upId", loginUser.getUserId());
                deleteMap.put("fansId", upId);

                redisUtil.setRemove(upId + "follow",loginUser.getUserId());
            }
            int isSuccess = subscribeMapper.deleteByMap(deleteMap);
            if (isSuccess > 0) {
                return true;
            }
        }
        return false;

    }

    public Subscribe getSubscribeByUpId(long upId) {
        User loginUser = userFeign.getLoginUser();
        String follow = "";
        boolean isFans = redisUtil.sHasKey(loginUser.getUserId() + "fansId", upId);
        boolean isUp = redisUtil.sHasKey(loginUser.getUserId() + "upId", upId);
        if (isFans && isUp) {
            follow = "3";
        }
        else if (isFans) {
            follow = "2";
        }
        else if (isUp) {
            follow = "1";
        }
        else  {
            follow="0";
        }
        return new Subscribe().setFansId(userFeign.getLoginUser().getUserId()).setUpId(upId).setFollow(follow);
    }
}
